import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    meta: { title: '主页' },
    component: () => import('@/views/Home.vue'),
    redirect: '/home/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: {
          title: '首页',
          icon: 'dashboard'
        }
      },
      {
        path: 'example',
        redirect: '/example/table',
        name: 'Example',
        meta: {
          title: '组件',
          icon: 'el-icon-s-help'
        },
        children: [
          {
            path: 'table',
            name: 'Table',
            component: () => import('@/views/table/index'),
            meta: { title: '表格', icon: 'table' }

          },
          {
            path: 'form',
            name: 'Form',
            component: () => import('@/views/form/index'),
            meta: { title: '表单', icon: 'table' }
          },
          {
            path: 'tree',
            name: 'Tree',
            component: () => import('@/views/tree/index'),
            meta: { title: 'Tree', icon: 'tree' }

          }
        ]
      },
      {
        path: 'Dy-Component',
        name: 'Dy-Component',
        meta: {
          title: '动态组件'
        },
        component: () => import('@/views/Dy-Component/index')
      },
      {
        path: 'text',
        name: 'Text',
        meta: {
          title: '富文本'
        },
        component: () => import('@/views/text/index')

      },
      {
        path: 'user',
        name: 'User',
        meta: {
          title: '个人中心'
        },
        component: () => import('@/views/user/index')
      }
    ]
  }

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// router.beforeEach((to, from, next) => {
//   console.log(to.path)
//   if (to.meta.title) {
//     document.title = to.meta.title
//   }
//   if (to.path === '/login' || to.path === '/') {
//     next()
//   }
//   // 获取token
//   // const tokenStr = sessionStorage.getItem('token')
//   // if (!tokenStr) {
//   //   return next('/login')
//   // } else {
//   //   next()
//   // }
// })

export default router
